package org.dawn.deveco.plugin.resourceeditor.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class OpenResourceEditorAction extends AnAction {

    public static final Logger LOG = Logger.getInstance(OpenResourceEditorAction.class);

    private static final String FILE_PATH = System.getProperty("user.home") + "/.idea-plugin-data.txt";

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        //获取当前在操作的工程上下文
        Project project = e.getData(PlatformDataKeys.PROJECT);

        //获取当前操作的类文件
//        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        //获取当前类文件的路径
//        String classPath = psiFile.getVirtualFile().getPath();
        String title = "Hello World!";

        //显示对话框
//        Messages.showMessageDialog(project, project.getBasePath(), title, Messages.getInformationIcon());

        LOG.info(project.getBasePath() + title);


    }

    private void saveNameToFile(String name) {
        // Implement the logic to save name to a local file
        System.out.println("Saving name to file: " + name);
        try (FileWriter writer = new FileWriter(FILE_PATH)) {
            writer.write(name);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private String loadNameFromFile() {
        try {
            return new String(Files.readAllBytes(Paths.get(FILE_PATH)));
        } catch (IOException ex) {
            ex.printStackTrace();
            return "";
        }
    }
}
